// pages/detail/lindex.js
import ajax from "../network/index"
Page({

  /**
   * 页面的初始数据
   */
  data: {
    result: [],
    money: 0,
    taskDetail: false, //详情页
    totalSize: 5,
    pageDetal: 1,
    showTime: false,
    startDate: '',
    timeArr: [Date.now(), Date.now() + (1000 * 60 * 60 * 24)],
    minDates: Date.now(),
    maxDates: Date.now(),
    endDate: "",
    postParmas: {
      searchVal: '',
      pageNum: 1,
      userId: '',
      type: "1003",
      searchDate: '',
      searchEndDate: ''
    },
    option3: [{
        text: '全部',
        value: '1001'
      },
      {
        text: '已报',
        value: '1002'
      },
      {
        text: '未报',
        value: '1003'
      },
      {
        text: '已拒绝',
        value: '1004'
      },
    ],
    option4: [{
      text: "选择单个日期",
      value: "1"
    }, {
      text: "选择日期区间",
      value: "2"
    }]
  },

  pageDetal(e) {
    let code = e.currentTarget.dataset.code
    wx.navigateTo({
      url: "/pages/Taskdetail/lindex?code=" + code
    })
  },
  selectChange(e) {
    let index = e.currentTarget.dataset.index
    if (index == 0) {
      this.setData({
        'postParmas.type': e.detail,
        "postParmas.pageNum": 1
      })
    }
    this.getDetailList(this.data.postParmas, 'header')
    wx.pageScrollTo({
      duration: 0,
      scrollTop: 0
    })
  },
  openTime() {
    this.setData({
      showTime: true
    })
  },
  onConfirmTwo() {
    let searchDate = this.formatDate(this.data.timeArr[0])
    let searchEndDate = this.formatDate(this.data.timeArr[1])
    this.setData({
      'postParmas.searchDate': searchDate,
      'postParmas.searchEndDate': searchEndDate,
      startDate: this.formatDate(this.data.timeArr[0], 1, 'gang'),
      endDate: this.formatDate(this.data.timeArr[1], 1, 'gang'),
      "postParmas.pageNum": 1,
      timeType: '',
      showTime: false
    })
    this.selectComponent('#clickTime').toggle()
    this.getDetailList(this.data.postParmas, 'header')
    wx.pageScrollTo({
      duration: 0,
      scrollTop: 0
    })

  },
  onSelect(e) {
    let arr = e.detail
    this.setData({
      timeArr: arr
    })
  },
  //关闭弹窗
  onCloseTwo() {
    this.setData({
      showTime: false,
      // 'postParmas.searchDate': '',
      // 'postParmas.searchEndDate': '',
      // startDate: '',
      // endDate: '',
    })
    this.selectComponent('#clickTime').toggle()
    // this.getDetailList(this.data.postParmas, 'header')
    // wx.pageScrollTo({
    //   duration: 0,
    //   scrollTop: 0
    // })
  },
  //清空
  resetTime() {
    this.setData({
      showTime: false,
      'postParmas.searchDate': '',
      'postParmas.searchEndDate': '',
      startDate: '',
      endDate: '',
    })
    this.selectComponent('#clickTime').toggle()
    this.getDetailList(this.data.postParmas, 'header')
    wx.pageScrollTo({
      duration: 0,
      scrollTop: 0
    })
  },

  formatDate(date, type, e) {

    date = new Date(date);
    let year = date.getFullYear()
    let month = date.getMonth() + 1
    let day = date.getDate()

    month = month < 10 ? '0' + month : month
    day = day < 10 ? '0' + day : day
    if (type == 1) {
      if (e == 'gang') {
        return `${month}月${day}日`
      }
      return `${month}-${day}`
    } else if (type == 2) {

      return `${year}-${month}`

    } else {
      return `${year}-${month}-${day}`
    }
  },

  //顶部的按钮选择事件
  // filtrate(e) {

  //   let index = e.detail.index
  //   let date = e.detail.date
  //   this.setData({
  //     'postParmas.type': index,
  //     'postParmas.searchDate': date,
  //     "postParmas.pageNum": 1
  //   })

  //   this.getDetailList(this.data.postParmas, 'header')
  //   wx.pageScrollTo({
  //     duration: 0,
  //     scrollTop: 0
  //   })
  // },
  //搜索内容

  search(e) {
    let {
      value
    } = e.detail
    this.setData({
      'postParmas.pageNum': 1
    })
    let data = this.data.postParmas
    console.log(data)
    data.searchVal = value
    wx.showLoading({
      title: '加载数据',
    })
    ajax({
      url: "/api/reimbursed/userReimbursedRecord",
      method: "POST",
      header: {
        "content-type": "application/x-www-form-urlencoded"
      },
      data
    }).then(v => {

      this.setData({
        totalSize: v.data.totalSize,
        result: v.data
      })
      wx.hideLoading({
        success: (res) => {},
      })
    }).catch(v => {
      console.log(v)
      wx.hideLoading({
        success: (res) => {},
      })
    })

  },
  present() {
    wx.navigateTo({
      url: '/pages/present/lindex'
    })
  },
  getDetailList(data, options = 0) {
    wx.showLoading({
      title: '加载中...',
      mask: true
    })
    ajax({
      url: "/api/reimbursed/userReimbursedRecord",
      method: "POST",
      header: {
        "content-type": "application/x-www-form-urlencoded"
      },
      data
    }).then(v => {
      if (v.code != 200) {
        wx.showToast({
          icon: "none",
          title: v.msg,
          duration: 1500,
          success: function () {
            wx.clearStorageSync()
          }
        })
      } else {
        if (v.data == null) {
          this.setData({
            result: []
          })
        } else {
          if (options == 0) {

            if (!(this.data.result.length == '')) {

              let olddata = this.data.result.list
              let newData = v.data.list
              let currentData = [...olddata, ...newData]
              v.data.list = currentData
              this.setData({
                result: v.data,
                totalSize: v.data.totalSize
              })
            } else {
              this.setData({
                result: v.data,
                totalSize: v.data.totalSize
              })
            }
          } else {

            this.setData({
              result: v.data,
              totalSize: v.data.totalSize
            })
          }
        }
      }
      wx.hideLoading()
      wx.stopPullDownRefresh()
    }).catch(v => {
      wx.hideLoading()
      wx.stopPullDownRefresh(),
        wx.showToast({
          title: '失败',
          icon: "none"
        })
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onLoad: function () {
    var _this = this
    let str = this.formatDate(Date.now(), 2) + '-01'
    str = str.replace(/-/g, '/')
    let minDates = new Date(str).getTime() - (1000 * 60 * 60 * 24 * 30 * 5)
    let maxDates = new Date(str).getTime() + (1000 * 60 * 60 * 24 * 31)
    var userId = wx.getStorageSync('userId')
    if (userId) {
      this.setData({
        'postParmas.userId': userId,
        minDates,
        maxDates
      })
      this.getDetailList(_this.data.postParmas, '第一次进')
      wx.login({
        timeout: 5000,
        success(v) {
          let {
            code
          } = v
          ajax({
            url: "/api/user/userBindOpenId",
            method: "POST",
            header: {
              "content-type": "application/x-www-form-urlencoded"
            },
            data: {
              userId: _this.data.postParmas.userId,
              openId: code
            }
          }).then(v => {
            console.log(v)
          })
        }
      })
    }
  },
  onShow: function () {
    let _this = this
    var userId = wx.getStorageSync('userId')
    if (!userId) {
      wx.showToast({
        title: '请先登录',
        icon: "none"
      })
      wx.reLaunch({
        url: '/pages/login/index',
      })
      return
    }
    
    // 实名认证--------------------------------
    // if (wx.getStorageSync('isAuthentication') != 1) {
    //   wx.showToast({
    //     title: '请先完成实名认证！',
    //     icon: "none"
    //   })
    //   wx.redirectTo({
    //     url: '/pages/signIn/index',
    //   })
    //   return
    // }
    // 实名认证--------------------------------




    //获取列表详情
    //获取路由
    if (!this.data.postParmas.pageNum > 1) {
      this.setData({
        'postParmas.pageNum': 1,
      })
      this.getDetailList(this.data.postParmas, '第一次进')
      wx.pageScrollTo({
        duration: 0,
        scrollTop: 0
      })
    }
    wx.removeStorageSync('cacheList')
  },
  onShareAppMessage:function(){},
  onShareTimeline:function(){},

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {


  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.setData({
      'postParmas.pageNum': 1,
    })
    this.setData({

    })
    wx.showLoading({
      title: '获取数据中',
    })
    this.getDetailList(this.data.postParmas, "pull")

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    let data = this.data.postParmas
    if (this.data.totalSize == 5) {
      data.pageNum++
      this.setData({
        'postParmas.pageNum': data.pageNum
      })
      this.getDetailList(data)
      wx.showLoading({
        title: '正在加载更多数据',
      })
    } else {
      wx.showToast({
        title: '没有更多数据了',
        icon: "none"
      })
    }
  },

  /**
   * 用户点击右上角分享
   */
})